<?php
session_start();

$_SESSION['dbhost'] = 'engr-db.engr.oregonstate.edu:3307';
$_SESSION['dbname'] = 'funkecd';
$_SESSION['dbuser'] = 'funkecd';
$_SESSION['dbpass'] = 'ktrDtj1b';

$mysql_handle = mysql_connect($_SESSION['dbhost'], $_SESSION['dbuser'], $_SESSION['dbpass'])
    or die("Error connecting to database server");

mysql_select_db($_SESSION['dbname'], $mysql_handle)
    or die("Error selecting database: $dbname");

$regLName = '^[a-zA-Z\'\.]{1,20}.[[:space:]][a-zA-Z0-9\'\.]{1,5}[[:space:]]*$'; //regex for the last name
$regFName = '^[a-zA-Z\'\.][[:space:]]*$'; //regex for the first name
$regText= '^[a-zA-Z0-9[:space:]\.\,\;\:#]{1,50}$'; //generic regex
$regSText = '[a-zA-Z0-9[:space:]\.\,\;\:#]{1,10}$';
$regNum= '^[0-9]{1,20}$'; //for numbers
$regSNum= '^[0-9]{1,3}$'; //for small numbers
$err = false;

$pid = $_SESSION['pid'];

$gravida =  $_POST['gravida'];
if( !ereg($regSNum, $gravida) && $gravida != null)
{
	$err = true;
	$_SESSION['gravErr'] = true;
	$_SESSION['regexErr'] = true;
}
else if ($gravida == null )
{
	$gravida = "'NULL'";
}
else
{
	$_SESSION['gravida'] = $gravida;
}
$para = $_POST['para'];
if( !ereg($regSNum, $para) && $para != null)
{
	$err = true;
	$_SESSION['Paraerr'] = true;
	$_SESSION['regexErr'] = true;
}

else if ($para == null )
{
	$para = "'NULL'";
}
else
{
	$_SESSION['para'] = $para;
}
$gestation_age = $_POST['gestation_age'];
if( !ereg($regSNum, $gestation_age) && $gestation_age  != null)
{
	$err = true;
	$_SESSION['GAgeerr'] = true;
	$_SESSION['regexErr'] = true;
}
else if ($gestation_age  == null)
{
	$gestation_age = "'NULL'";
}
else
{
	$_SESSION['gestation_age'] = $gestation_age ;
}	
$reason_for_cs = $_POST['reason_for_cs'];
if( !ereg($regText, $reason_for_cs) && $reason_for_cs != null)
{
	$err = true;
	$_SESSION['RFCSerr'] = true;
	$_SESSION['regexErr'] = true;
} 
else if ($reason_for_cs == null)
{
	$reason_for_cs = " ";
}
else
{
	$_SESSION['reason_for_cs'] = $reason_for_cs;
}
$current_medications = $_POST['current_medications'];
if( !ereg($regText, $current_medications) && $current_medications != null)
{
	$err = true;
	$_SESSION['CurMerr'] = true;
	$_SESSION['regexErr'] = true;
} 
else if ($current_medications == null)
{
	$current_medications = " ";
}
else
{
	$_SESSION['current_medications'] = $current_medications;
}
$allergies =  $_POST['allergies'];
if( !ereg($regText, $allergies ) && $allergies != null)
{
	$err = true;
	$_SESSION['AllEerr'] = true;
	$_SESSION['regexErr'] = true;
}
else if ($allergies == null)
{
	$allergies = " ";
}
else
{
	$_SESSION['allergies'] = $allergies;
}
$health_during_pregnacny =  $_POST['health_during_pregnancy'];
if( !ereg($regText, $health_during_pregnacny ) && $health_during_pregnacny != null)
{
	$err = true;
	$_SESSION['HDPRerr'] = true;
	$_SESSION['regexErr'] = true;
}
else if ($health_during_pregnacny == null)
{
	$health_during_pregnacny = " ";
}
else
{
	$_SESSION['health_during_pregnacny'] = $health_during_pregnacny;
}
$past_major_illnesses = $_POST['past_major_illnesses'];
if( !ereg($regText, $past_major_illnesses) && $past_major_illnesses != null)
{
	$err = true;
	$_SESSION['PMILerr'] = true;
	$_SESSION['regexErr'] = true;
}
else if ($past_major_illnesses == null)
{
	$past_major_illnesses = " ";
}
else
{
	$_SESSION['past_major_illnesses'] = $past_major_illnesses;
}
$membrane_rupture_color =  $_POST['membrane_rupture_color'];
if( !ereg($regSText, $membrane_rupture_color) && $membrane_rupture_color != null)
{
	$err = true;
	$_SESSION['MRCOerr'] = true;
	$_SESSION['regexErr'] = true;
}
else if ($membrane_rupture_color == null)
{
	$membrane_rupture_color = " ";
}
else
{
	$_SESSION['membrane_rupture_color'] = $membrane_rupture_color;
}
$membrane_rupture_amount = $_POST['membrane_rupture_amount'];
if( !ereg($regSNum, $membrane_rupture_amount) && $membrane_rupture_amount != null)
{
	$err = true;
	$_SESSION['MRAMerr'] = true;
	$_SESSION['regexErr'] = true;
}
else if ($membrane_rupture_amount == null)
{
	$membrane_rupture_amount = "'NULL'";
}
else
{
	$_SESSION['membrane_rupture_amount'] = $membrane_rupture_amount;
}
$cervix_position = $_POST['cervix_position'];
if( !ereg($regText, $cervix_position) && $cervix_position != null)
{
	$err = true;
	$_SESSION['CPOSerr'] = true;
	$_SESSION['regexErr'] = true;
}
else if ($cervix_position  == null)
{
	$cervix_position  = " ";
}
else
{
	$_SESSION['cervix_position'] = $cervix_position;
}
$uterus_size = $_POST['uterus_size'];
if( !ereg($regSNum, $uterus_size)&& $uterus_size != null)
{
	$err = true;
	$_SESSION['USIZerr'] = true;
	$_SESSION['regexErr'] = true;
}
else if ($uterus_size == null)
{
	$uterus_size = "'NULL'";
}
else
{
	$_SESSION['uterus_size'] = $uterus_size;
}	
$uterus_lie = $_POST['uterus_lie'];
if( !ereg($regText, $uterus_lie) && $uterus_lie != null)
{
	$err = true;
	$_SESSION['ULIEerr'] = true;
	$_SESSION['regexErr'] = true;
}
else if ($uterus_lie == null)
{
	$uterus_lie = " ";
}
else
{
	$_SESSION['uterus_lie'] = $uterus_lie;
}
$presentation = $_POST['presentation'];
if( !ereg($regSText, $presentation) && $presentation != null)
{
	$err = true;
	$_SESSION['PRESerr'] = true;
	$_SESSION['regexErr'] = true;
}
else if ($presentation == null)
{
	$presentation = " ";
}
else
{
	$_SESSION['presentation'] = $presentation;
}
$dilation = $_POST['dilation'];
if( !ereg($regSText, $dilation ) && $dilation != null)
{
	$err = true;
	$_SESSION['DILAerr'] = true;
	$_SESSION['regexErr'] = true;
}
else if ( $dilation == null)
{
	$dilation = " ";
}
else
{
	$_SESSION['dilation'] = $dilation;
}
$contraction_interval =  $_POST['contraction_interval'];
if( !ereg($regSNum, $contraction_interval) && $contraction_interval != null)
{
	$err = true;
	$_SESSION['CINTerr'] = true;
	$_SESSION['regexErr'] = true;
}
else if ( $contraction_interval == null)
{
	$contraction_interval = "'NULL'";
}
else
{
	$_SESSION['contraction_interval'] = $contraction_interval;
}
$contraction_length = $_POST['contraction_length'];
if( !ereg( $regSNum, $contraction_length) && $contraction_length != null)
{
	$err = true;
	$_SESSION['CLENerr'] = true;
	$_SESSION['regexErr'] = true;
}
else if ($contraction_length == null)
{
	$contraction_length = "'NULL'";
}
else
{
	$_SESSION['contraction_length'] = $contraction_length;
}
$contraction_intensity =  $_POST['contraction_intensity'];
if( !ereg($regSNum, $contraction_intensity) && $contraction_intensity != null)
{
	$err = true;
	$_SESSION['CINYerr'] = true;
	$_SESSION['regexErr'] = true;
}
else if ($contraction_intensity == null)
{
	$contraction_intensity = "'NULL'";
}
else
{
	$_SESSION['contraction_intensity'] = $contraction_intensity;
}

$data = "$pid, $gravida, $para, $gestation_age, '$reason_for_cs', '$current_medications', '$allergies', '$health_during_pregnacny' , '$past_major_illnesses' , NULL, '$membrane_rupture_color', $membrane_rupture_amount, '$cervix_position', $uterus_size, '$uterus_lie', '$presentation', '$dilation', $contraction_interval, $contraction_length, $contraction_intensity";

$pmquery = "INSERT INTO `Patient_Medical_Data` (
`pid` ,
`gravida` ,
`para` ,
`grestation_age` ,
`reason_for_cs` ,
`current_medications` ,
`allergies` ,
`health_during_pregnancy` ,
`past_major_illnesses` ,
`membrane_rupture_time` ,
`membrane_rupture_color` ,
`membrane_rupture_amount` ,
`cervix_position` ,
`uterus_size` ,
`uterus_lie` ,
`presentation` ,
`dilation` ,
`contraction_interval` ,
`contraction_length` ,
`contraction_intensity`
)
VALUES
(
$data
)
";
$probe = "SELECT * FROM `Patient_Medical_Data` WHERE `pid` = $pid";
$result = mysql_query($probe);
$row = mysql_fetch_array($result);

if ($err == false)
{
	if ($row['pid'] == $pid)
	{
	$delquery = "DELETE FROM `Patient_Medical_Data` WHERE `pid` = $pid";
	$result = mysql_query($delquery);
	}

	$result = mysql_query($pmquery);
	$_SESSION['inserted'] = true;
}
else
{
	$_SESSION['error'] = true;
}

mysql_close($mysql_handle);
header( 'Location: index.php?page=PatientMedicalInformation.php' ) ;

?>
